package net.jxtz.jxtzos.spider.process;

import lombok.SneakyThrows;
import lombok.extern.slf4j.Slf4j;
import net.jxtz.jxtzos.entity.spider.SpiderLogServe;
import net.jxtz.jxtzos.mapper.spider.IFangMapper;
import net.jxtz.jxtzos.spider.BaseSpiderProcessor;
import net.jxtz.jxtzos.spider.SpiderDefinition;
import net.jxtz.jxtzos.spider.SpiderFactory;
import net.jxtz.jxtzos.util.ResponseUtil;
import org.apache.commons.lang3.StringUtils;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;
import org.springframework.stereotype.Component;
import org.springframework.util.CollectionUtils;

import java.io.IOException;
import java.util.Objects;
import java.util.concurrent.TimeUnit;

/**
 * @author lmh
 * @version 1.0
 * @date 2021-12-05 14:42
 */

@Component
@Slf4j
public class SpiderIFangProcessor implements BaseSpiderProcessor {

    private final static int MAX_PAGE = 50;

    private final static int SLEEP_SECOND_TIME = 3;

    private static final CloseableHttpClient HTTP_CLIENT = HttpClients.createDefault();

    private IFangMapper iFangMapper;

    public static final String SPIDER_NAME = "ifang";

    public SpiderIFangProcessor(IFangMapper iFangMapper) {
        this.iFangMapper = iFangMapper;
        final SpiderFactory instance = SpiderFactory.getInstance();
        instance.putSpiderProcess("ifang", this);
    }

    @Override
    public void start(SpiderLogServe spiderLogServe, SpiderDefinition spiderDefinition) throws IOException {
        int page = 1;
        process("http://lp.lfang.com/search/e1_p" + page++ + "_y1_z0.htm");
    }

    private void process(String url) {
        final String s = httpClient(url);
        if (StringUtils.isBlank(s)) {
            return;
        }
        final Document parse = Jsoup.parse(s);
        final Elements mt20 = parse.getElementsByClass("mt20");
        if (Objects.nonNull(mt20) && CollectionUtils.isEmpty(mt20)) {
            log.info(url + "，结果为空");
        } else {
            final Element element = mt20.get(3);
            final Elements houseDLine2 = element.getElementsByClass("house d_Line2");
            for (Element element1 : houseDLine2) {
                // 进入详情页
                final String elementsByAttribute = element1.getElementsByTag("a").get(0).attr("href");
                final String s1 = httpClient(elementsByAttribute);
                final Document parse1 = Jsoup.parse(s1);
                final Element w695_left = parse1.getElementsByClass("w695 left").get(0);
                final String attr = w695_left.getElementsByTag("a").get(0).attr("href");
                System.out.println(attr);
            }
        }
    }

    @SneakyThrows
    private String httpClient(String url) {
        TimeUnit.SECONDS.sleep(SLEEP_SECOND_TIME);
        HttpGet httpGet = new HttpGet(url);
        httpGet.setHeader("User-Agent", "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/96.0.4664.55 Safari/537.36");
        httpGet.setHeader("Accept", "text/html,application/xhtml+xml,application/xml;q=0.9,image/avif,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3;q=0.9");
        httpGet.setHeader("Accept-Encoding", "gzip, deflate");
        httpGet.setHeader("Accept-Language", "Accept-Language");
        httpGet.setHeader("Cache-Control", "no-cache");
        httpGet.setHeader("Connection", "keep-alive");
        httpGet.setHeader("Host", "lp.lfang.com");
        httpGet.setHeader("Referer", "http://lp.lfang.com/search/e1_p2_y1_z0.htm");
        httpGet.setHeader("Cookie", "UM_distinctid=17b5c47c251217-0ab12ebe16b5e2-35667c03-1aeaa0-17b5c47c2526f4; CNZZDATA2853163=cnzz_eid%3D1988922385-1637626924-%26ntime%3D1638677830; CNZZDATA2963972=cnzz_eid%3D265833038-1637628717-%26ntime%3D1638679976; lfbh=%5B%7B%22id%22%3A%221222%22%2C%22title%22%3A%22%u8054%u53D1%u4E07%u79D1%u6D77%u4E0A%u660E%u6708%22%2C%22url%22%3A%22http%3A//lp.lfang.com/Items/2019-8/38419746.htm%22%2C%22cover%22%3A%22http%3A//lp.lfang.com/p/images/1f3f96056e13929d51435584ef35d534.jpg%22%2C%22price%22%3A%2216500%20%u5143/%u33A1%22%2C%22region%22%3A%22%u9AD8%u65B0%u533A%22%2C%22r_url%22%3A%22http%3A//lp.lfang.com/search/a5.htm%22%7D%2C%7B%22id%22%3A%221061%22%2C%22title%22%3A%22%u534E%u6DA6%u4E07%u8C61%u57CE%22%2C%22url%22%3A%22http%3A//lp.lfang.com/Items/2017-11/79265138.htm%22%2C%22cover%22%3A%22http%3A//lp.lfang.com/p/images/4a9e2bf7ae4a0028f8e78a5e1d6ee8d9.jpg%22%2C%22price%22%3A%2213000%20%u5143/%u33A1%22%2C%22region%22%3A%22%u7EA2%u8C37%u6EE9%u533A%22%2C%22r_url%22%3A%22http%3A//lp.lfang.com/search/a7.htm%22%7D%2C%7B%22id%22%3A%221383%22%2C%22title%22%3A%22%u4E07%u79D1%u6C5F%u53F3%u4E1C%u65B9%22%2C%22url%22%3A%22http%3A//lp.lfang.com/Items/2021-9/51271342.htm%22%2C%22cover%22%3A%22http%3A//lp.lfang.com/p/images/fa42576a8d0adfbf4f9dbb9819803c14.jpg%22%2C%22price%22%3A%2211500%20%u5143/%u33A1%22%2C%22region%22%3A%22%u5357%u660C%u53BF%22%2C%22r_url%22%3A%22http%3A//lp.lfang.com/search/a11.htm%22%7D%2C%7B%22id%22%3A%221363%22%2C%22title%22%3A%22%u65ED%u8F89%u4E2D%u5FC3%u6C34%u6295%u5FA1%u5E9C%22%2C%22url%22%3A%22http%3A//lp.lfang.com/Items/2021-4/73255720.htm%22%2C%22cover%22%3A%22http%3A//lp.lfang.com/p/images/86833817a79d9744b288cb8ed0a905a6.jpg%22%2C%22price%22%3A%2213500%20%u5143/%u33A1%22%2C%22region%22%3A%22%u7ECF%u5F00%u533A%22%2C%22r_url%22%3A%22http%3A//lp.lfang.com/search/a8.htm%22%7D%5D");
        final CloseableHttpResponse execute = HTTP_CLIENT.execute(httpGet);
        return ResponseUtil.entityToString(execute.getEntity());
    }
}
